//
//  BaseTabbarVC.m
//  MovieDate
//
//  Created by 杨运涛 on 16/3/11.
//  Copyright © 2016年 MovieDate. All rights reserved.
//

#import "BaseTabbarVC.h"
@interface BaseTabbarVC ()

@end

@implementation BaseTabbarVC

- (void)viewWillLayoutSubviews {
    UIView *bgView = [[UIView alloc] initWithFrame:self.tabBar.bounds];
    bgView.backgroundColor = [UIColor colorWithRed:0/255.0f green:0/255.0f blue:0/255.0f alpha:0.7f];
    bgView.alpha = 0.8;
    [self.tabBar insertSubview:bgView atIndex:0];
    self.tabBar.opaque = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取用户详细信息
    [self requestUserMessage];
    
    NSArray *arrayStoryboard = [NSArray arrayWithObjects:@"See", @"Date", @"Social", @"Mine", nil];
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < arrayStoryboard.count; i++) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:[arrayStoryboard objectAtIndex:i] bundle:nil];
        UIViewController *navi = [storyboard instantiateInitialViewController];
        [arr addObject:navi];
    }
    self.viewControllers = arr;
    
    //NSArray *arrayTitle = [NSArray arrayWithObjects:@"想看", @"想约", @"社交", @"我的", nil];
    NSArray *arrayImg = [NSArray arrayWithObjects:@"tabbar_1", @"tabbar_0", @"tabbar_2", @"tabbar_3", nil];
    NSArray *arrayImgSelect = [NSArray arrayWithObjects:@"tabbar_1_s", @"tabbar_0_s", @"tabbar_2_s", @"tabbar_3_s", nil];
    
    for (int i = 0; i < arrayStoryboard.count; i++) {
     UITabBarItem *item = [self.tabBar.items objectAtIndex:i];
        
       // [item setTitle:[arrayTitle objectAtIndex:i]];
        item.title = nil;
        [item setImageInsets:UIEdgeInsetsMake(5.0, 0.0, -5.0, 0.0)];
        item.selectedImage = [[UIImage imageNamed:[arrayImgSelect objectAtIndex:i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.image = [[UIImage imageNamed:[arrayImg objectAtIndex:i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
       
    }
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:178/255.0f green:178/255.0f blue:178/255.0f alpha:1], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];

    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAddLikeAction:) name:NOTI_ADD_ATTENTION object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notiAddMovieAction:) name:NOTI_ADD_MOVIE object:nil];
}

- (void)notiAddLikeAction:(NSNotification *)notification {
    UIViewController *navi = [self.viewControllers objectAtIndex:2];
    navi.tabBarItem.badgeValue = @"";
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setBool:YES forKey:@"add_like"];
    [userDefault synchronize];
}

- (void)notiAddMovieAction:(NSNotification *)notification {
    UIViewController *navi = [self.viewControllers objectAtIndex:2];
    navi.tabBarItem.badgeValue = @"";
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setBool:YES forKey:@"add_movie"];
    [userDefault synchronize];
}

-(void)requestUserMessage{

   [TDNetwork requsetUseMessagePostUrl:useGetMessageIP complete:^(BOOL succeed, NSString *ret, NSDictionary *dictionary) {
       if (succeed) {
           
           NSDictionary *dic = [dictionary valueForKey:@"res"];
           NSLog(@" 用户信息是： %@",dic);
            User *user = [User defaultUser];
           if (dic) {
               user.uid = [dic valueForKey:@"uid"];
               user.headImage = [dic valueForKey:@"headImage"];
               user.nickName = [dic valueForKey:@"nickName"];
               user.userAge = [dic valueForKey:@"userAge"];
               user.userSex = [dic valueForKey:@"userSex"];
               user.zambia = [dic valueForKey:@"zambia"];
               user.faceScore = [dic valueForKey:@"faceScore"];
               user.fans  = [dic valueForKey:@"fans"];
               user.movieLabel = [dic valueForKey:@"movieLabel"];
               user.commentCount = [dic valueForKey:@"commentCount"];
               user.commentStart = [dic valueForKey:@"commentStart"];
               user.commentTotalScore = [dic valueForKey:@"commentTotalScore"];
               user.phone = [dic valueForKey:@"phone"];
               user.pwd =  [dic valueForKey:@"pwd"];
               user.beReprotedCount = [dic valueForKey:@"beReportedCount"];
               user.appointmentCount = [dic valueForKey:@"appointmentCount"];
               user.breakCount = [dic valueForKey:@"breakCount"];
               user.signature = [dic valueForKey:@"signature"];
               NSString *labels =  [dic valueForKey:@"labels"];
               @try {
                   [labels stringByReplacingOccurrencesOfString:@"\\U" withString:@"!u"];
                   [labels stringByReplacingOccurrencesOfString:@"\\" withString:@""];
                   [labels stringByReplacingOccurrencesOfString:@"!u" withString:@"\\u"];
                   NSString*labels2 =  [labels stringByReplacingOccurrencesOfString:@"\'" withString:@"\""];
                   NSArray *arr=  [BaseTabbarVC arrWithJsonString:labels2];
                   [user.labels removeAllObjects];
                   for (NSDictionary *dic in arr)
                   {
                       MovieLabelModel *movieLbM = [[MovieLabelModel alloc]init];
                       movieLbM.name = [dic valueForKey:@"name"];
                       movieLbM.type = [NSString stringWithFormat:@"%@",[dic valueForKey:@"type"]];
                       movieLbM.uuid = [NSString stringWithFormat:@"%@",[dic valueForKey:@"uuid"]];
                       [user.labels addObject:movieLbM];
                   }
               } @catch (NSException *exception) {
               } @finally {
               }
               
           }
       }else{
       }
   }];
}

+ (NSMutableArray *)arrWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
   NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return arr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
